package org.example.lotterysystem.service.postwinning.operater;

import cn.hutool.core.date.DateUtil;
import org.example.lotterysystem.common.utils.MailUtil;
import org.example.lotterysystem.dao.dataobject.WinningRecordDO;
import org.example.lotterysystem.service.enums.ActivityPrizeTiersEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class EmailPost extends AbstractPostWinning{

    @Autowired
    private MailUtil mailUtil;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public void execute(List<WinningRecordDO> records) {
        // 批量发送或并行发送
        records.parallelStream().forEach(record -> {
            String context = buildEmailContent(record);
            mailUtil.sendSampleMail(record.getWinnerEmail(), "中奖通知", context);
        });
    }

    private String buildEmailContent(WinningRecordDO record) {
        return String.format("Hi，%s。恭喜你在%s活动中获得%s：%s。获奖时间为%s，请尽快领取您的奖励！",
                record.getWinnerName(),
                record.getActivityName(),
                ActivityPrizeTiersEnum.forName(record.getPrizeTier()).getMessage(),
                record.getPrizeName(),
                DateUtil.formatTime(record.getWinningTime()));
    }
}
